// course/pages/courseOnline/courseOnline.js
const {
    BASE_URL
} = require('../../../http/config');

Page({
    data: {
        BASE_URL,
        titles: [],
        titleIndex: 0,
        chanceIndex: 1,
        updown: 1,
        downMenu: false,
        intervalMenu: false,
        interval: 1,
        bodys: [],
        pageNum: 1,
        pageSize: 8,
        hasMore: true
    },

    onLoad(options) {
        this.getCourseClassify();
    },

    getCourseClassify() {
        wx.showLoading({
            title: '加载中'
        });
        wx.request({
            url: BASE_URL + '/courseClassify/all',
            method: 'GET',
            success: (res) => {
                wx.hideLoading();
                if (res.statusCode === 200 && res.data && res.data.data) {
                    const classifyList = res.data.data.list || [];
                    if (classifyList.length > 0) {
                        this.setData({
                            titles: classifyList,
                            titleIndex: 0,
                            pageNum: 1,
                            hasMore: true
                        }, () => {
                            this.loadCourseList(false);
                        });
                    }
                } else {
                    wx.showToast({
                        title: '获取分类失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                wx.hideLoading();
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
                console.error('获取课程分类失败:', err);
            }
        });
    },

    loadCourseList(append = false) {
        const {
            BASE_URL,
            titles,
            titleIndex,
            pageNum,
            pageSize
        } = this.data;
        const selectedId = titles[titleIndex].id;

        wx.request({
            url: `${BASE_URL}/course/all?pageNum=${pageNum}&pageSize=${pageSize}&courseClassify=${selectedId}`,
            method: 'GET',
            success: (res) => {
                const newList = res.data.data.list.map(item => ({
                    ...item,
                    thumbnail: item.thumbnail 
                }));

                this.setData({
                    bodys: append ? this.data.bodys.concat(newList) : newList,
                    hasMore: newList.length === pageSize
                });
            },
            fail: (err) => {
                console.error("加载课程失败：", err);
            }
        });
    },

    changeTitle(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            titleIndex: index,
            pageNum: 1,
            hasMore: true
        }, () => {
            this.loadCourseList(false);
        });
    },

    changeSort(e) {
        let index = e.target.dataset.index;
        this.setData({
            chanceIndex: index
        });
        if (index == 4) {
            this.setData({
                intervalMenu: false,
                downMenu: !this.data.downMenu
            });
        } else if (index == 5) {
            this.setData({
                downMenu: false,
                intervalMenu: !this.data.intervalMenu
            });
        } else {
            this.setData({
                downMenu: false,
                intervalMenu: false
            });
        }
    },

    changePrice(e) {
        let updown = e.target.dataset.updown;
        this.setData({
            updown: updown,
            downMenu: false
        });
    },

    changeInterval(e) {
        let interval = e.target.dataset.interval;
        this.setData({
            interval: interval
        });
    },

    sureInterval() {
        this.setData({
            intervalMenu: false
        });
    },

    jumpUrl(e) {
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: url
        });
    },

    jumpCourseDetail(e) {
        const courseId = e.currentTarget.dataset.id;
        if (!courseId) {
            wx.showToast({
                title: '课程ID不存在',
                icon: 'none'
            });
            return;
        }
        wx.navigateTo({
            url: `/course/pages/courseDetail/courseDetail?id=${courseId}`
        });
    },

    onReachBottom() {
        if (!this.data.hasMore) {
            wx.showToast({
                title: '没有更多课程了',
                icon: 'none'
            });
            return;
        }

        this.setData({
            pageNum: this.data.pageNum + 1
        }, () => {
            this.loadCourseList(true); // 追加加载
        });
    },

    onReady() {},
    onShow() {},
    onHide() {},
    onUnload() {},
    onPullDownRefresh() {},
    onShareAppMessage() {}
});
